package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

import java.util.*;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/">1333. 餐厅过滤器</a>
 */
public class Solution1333 extends BaseSolution {

    public List<Integer> filterRestaurants(int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) {
        class Model {
            final int id;
            final int rate;

            public Model(int id, int rate) {
                this.id = id;
                this.rate = rate;
            }
        }
        PriorityQueue<Model> queue = new PriorityQueue<>((m1, m2) -> {
            int ro = m2.rate - m1.rate;
            return ro == 0 ? m2.id - m1.id : ro;
        });

        for (int[] val : restaurants) {
            if (val[2] == veganFriendly && val[3] < maxPrice && val[4] < maxDistance) {
                queue.offer(new Model(val[0], val[1]));
            }
        }

        List<Integer> rst = new LinkedList<>();
        while (!queue.isEmpty()) {
            rst.add(queue.poll().id);
        }
        return rst;
    }

    /*
    另有一种函数式变成方法，很简洁，但是速度较慢，大约是上边方案的一倍左右

    public List<Integer> filterRestaurants(int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) {
        return Stream.of(restaurants)
                .filter(r -> r[2] >= veganFriendly && r[3] <= maxPrice && r[4] <= maxDistance)
                .sorted((m1, m2) -> {
                    int ro = m2[1] - m1[1];
                    return ro == 0 ? m2[0] - m1[0] : ro;
                })
                .map(val -> val[0])
                .collect(Collectors.toList());
    }
     */

    public static void main(String[] args) {
        Solution1333 solution = new Solution1333();
    }
}